package com.ruoyi.media.invitationCode.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.media.utils.WxUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.invitationCode.domain.bo.MediaInvitationCodeBo;
import com.ruoyi.media.invitationCode.domain.vo.MediaInvitationCodeVo;
import com.ruoyi.media.invitationCode.domain.MediaInvitationCode;
import com.ruoyi.media.invitationCode.mapper.MediaInvitationCodeMapper;
import com.ruoyi.media.invitationCode.service.IMediaInvitationCodeService;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户邀请码Service业务层处理
 *
 * @author dqw
 * @date 2023-05-27
 */
@RequiredArgsConstructor
@Service
public class MediaInvitationCodeServiceImpl extends ServiceImpl<MediaInvitationCodeMapper,MediaInvitationCode> implements IMediaInvitationCodeService {

    private final MediaInvitationCodeMapper baseMapper;

    /**
     * 查询用户邀请码
     */
    @Override
    public MediaInvitationCodeVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户邀请码列表
     */
    @Override
    public TableDataInfo<MediaInvitationCodeVo> queryPageList(MediaInvitationCodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MediaInvitationCode> lqw = buildQueryWrapper(bo);
        Page<MediaInvitationCodeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户邀请码列表
     */
    @Override
    public List<MediaInvitationCodeVo> queryList(MediaInvitationCodeBo bo) {
        LambdaQueryWrapper<MediaInvitationCode> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaInvitationCode> buildQueryWrapper(MediaInvitationCodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaInvitationCode> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaInvitationCode::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaInvitationCode::getUserId, bo.getUserId());
        lqw.eq(bo.getUseUserName() != null, MediaInvitationCode::getUseUserName, bo.getUseUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getInvitationCode()), MediaInvitationCode::getInvitationCode, bo.getInvitationCode());
        lqw.eq(bo.getIsUse() != null, MediaInvitationCode::getIsUse, bo.getIsUse());
        lqw.eq(bo.getValidTime() != null, MediaInvitationCode::getValidTime, bo.getValidTime());
        lqw.eq(bo.getActivationTime() != null, MediaInvitationCode::getActivationTime, bo.getActivationTime());
        return lqw;
    }

    /**
     * 新增用户邀请码
     */
    @Override
    public Boolean insertByBo(MediaInvitationCodeBo bo) {
        MediaInvitationCode add = BeanUtil.toBean(bo, MediaInvitationCode.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户邀请码
     */
    @Override
    public Boolean updateByBo(MediaInvitationCodeBo bo) {
        MediaInvitationCode update = BeanUtil.toBean(bo, MediaInvitationCode.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaInvitationCode entity){
        //TODO 做一些数据校验,如唯一约束
//        LoginUser loginUser =  LoginHelper.getLoginUser();
//        entity.setUserId(loginUser.getUserId());
//        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除用户邀请码
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    /**
     * 根据天数创建激活码
     * @param dayNum
     * @return
     */
    @Override
    public boolean createInvitationCode(int dayNum) {
        //创建激活码
        String invitationCode = WxUtils.getRandomStr(16);
        System.out.println(invitationCode);
        //创建邀请码时间
        MediaInvitationCode mediaInvitationCode = new MediaInvitationCode();
        mediaInvitationCode.setId(invitationCode);
        validEntityBeforeSave(mediaInvitationCode);
        mediaInvitationCode.setValidTime(String.valueOf(dayNum));
        mediaInvitationCode.setInvitationCode(invitationCode);
        return save(mediaInvitationCode);

    }

}
